/*
 * This file is part of the BGDB package
 *
 * Copyright (C) 2013 Baligh GUESMI Personnel use.
 *
 * Contact: Baligh GUESMI <gasmiba@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include <bgutil.h>

/** \brief String containing the version information. */
const char *bg_version = VERSION;

/** \brief Call back function for handling a fatal error. */
void (*bg_fatal_func)(const char *message) = NULL;

/********************************************//**
 * \brief Allocate a region on memory.
 *
 * \param size size_t
 * \return void*
 *
 ***********************************************/
void *bg_malloc(size_t size){
  assert(size > 0 && size < INT_MAX);
  char *p;
  TCMALLOC(p, size);
  return p;
}
/********************************************//**
 * \brief Allocate a nullified region on memory.
 *
 * \param nmemb size_t
 * \param size size_t
 * \return void*
 *
 ***********************************************/
void *bg_calloc(size_t nmemb, size_t size){
  assert(nmemb > 0 && nmemb < INT_MAX && size > 0 && size < INT_MAX);
  char *p;
  TCCALLOC(p, nmemb, size);
  return p;
}
/********************************************//**
 * \brief Re-allocate a region on memory.
 *
 * \param ptr void*
 * \param size size_t
 * \return void*
 *
 ***********************************************/
void *bg_realloc(void *ptr, size_t size){
  assert(size >= 0 && size < INT_MAX);
  char *p;
  TCREALLOC(p, ptr, size);
  return p;
}
/********************************************//**
 * \brief Duplicate a region on memory.
 *
 * \param ptr const void*
 * \param size size_t
 * \return void*
 *
 ***********************************************/
void *bg_memdup(const void *ptr, size_t size){
  assert(ptr && size >= 0);
  char *p;
  TCMALLOC(p, size + 1);
  BG_MEMCPY(p, ptr, size);
  p[size] = '\0';
  return p;
}
/********************************************//**
 * \brief Duplicate a string on memory.
 *
 * \param str const void*
 * \return char*
 *
 ***********************************************/
char *bg_strdup(const void *str){
  assert(str);
  int size = strlen(str);
  char *p;
  TCMALLOC(p, size + 1);
  BG_MEMCPY(p, str, size);
  p[size] = '\0';
  return p;
}
/********************************************//**
 * \brief Free a region on memory.
 *
 * \param ptr void*
 * \return void
 *
 ***********************************************/
void bg_free(void *ptr){
  TCFREE(ptr);
}
